'\" t
.\"
.\" This file and its contents are supplied under the terms of the
.\" Common Development and Distribution License ("CDDL"), version 1.0.
.\" You may only use this file in accordance with the terms of version
.\" 1.0 of the CDDL.
.\"
.\" A full copy of the text of the CDDL should have accompanied this
.\" source.  A copy of the CDDL is also available via the Internet at
.\" http://www.illumos.org/license/CDDL.
.\"
.\"
.\" Copyright (c) 2013, Joyent, Inc. All rights reserved.
.\"
.TH GETIFADDRS 3SOCKET "Feb 1, 2022"
.SH NAME
getifaddrs, freeifaddrs \- get interface addresses
.SH SYNOPSIS
.nf
\fBcc\fR [ \fIflag\fR ... ] \fIfile\fR ... \fB-lsocket\fR \fB-lnsl\fR \
[ \fIlibrary\fR ... ]
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
.fi

.LP
.nf
\fBint\fR \fBgetifaddrs\fR(\fBstruct ifaddrs **\fR\fIifap\fR);
.fi

.LP
.nf
\fBvoid\fR \fBfreeifaddrs\fR(\fBstruct ifaddrs *\fR\fIifp\fR);
.fi

.SH DESCRIPTION
The \fBgetifaddrs\fR() function is used to obtain the list of network
interfaces on the local machine.  A reference to a linked list of \fBifaddrs\fR
structures, as defined in \fB<ifaddrs.h>\fR, is stored in the memory referenced
by \fIifap\fR.  Each structure in the list describes one network interface
address, and is of the form:

.sp
.in +2
.nf
struct ifaddrs {
    struct ifaddrs      *ifa_next;
    char                *ifa_name;
    uint64_t            ifa_flags;
    struct sockaddr     *ifa_addr;
    struct sockaddr     *ifa_netmask;
    union {
        struct sockaddr *ifu_broadaddr;
        struct sockaddr *ifu_dstaddr;
    } ifa_ifu;
    void                *ifa_data;
};
#define ifa_broadaddr   ifa_ifu.ifu_broadaddr
#define ifa_dstaddr     ifa_ifu.ifu_dstaddr
.fi
.in -2

.sp
.LP
The list is traversed by following the \fIifa_next\fR pointer.  This member is
\fBNULL\fR on the last structure in the list.

.sp
.LP
The \fIifa_name\fR member contains the interface name.

.sp
.LP
The \fIifa_flags\fR member contains the interface flags.

.sp
.LP
The \fIifa_addr\fR member references the address of the interface.  Use the
\fIsa_family\fR member of this structure to determine the format of the
address, as described in \fBsocket.h\fR(3HEAD).

.sp
.LP
The \fIifa_netmask\fR member references the netmask associated with ifa_addr,
or \fBNULL\fR if one is not set.

.sp
.LP
If the \fBIFF_BROADCAST\fR bit is set in \fIifa_flags\fR, then
\fIifa_broadaddr\fR is valid, or \fBNULL\fR if not present.  If the
\fBIFF_POINTOPOINT\fR bit is set, then \fIifa_dstaddr\fR is valid, or \fBNULL\fR
if not present.  These two flags are mutually exclusive; see \fBif_tcp\fR(4P)
for more information.

.sp
.LP
The \fIifa_data\fR member is specific to the address family. It is currently
only available for AF_LINK entries where it contains a pointer to the
\fBstruct if_data\fR (as defined in \fBif.h\fR(3HEAD)).

.sp
.LP
The memory used by \fBgetifaddrs\fR() to back the list is dynamically allocated.
It should be freed using \fBfreeifaddrs\fR().

.SH RETURN VALUES
If successful, \fBgetifaddrs\fR() returns the value \fB0\fR; otherwise it
returns \fB\(mi1\fR and sets \fIerrno\fR to indicate the error.

.SH ERRORS
The \fBgetifaddrs\fR() function may fail and set \fIerrno\fR for any of the
errors specified for the library routines \fBioctl\fR(2),
\fBsocket\fR(3SOCKET), and \fBmalloc\fR(3C).

.SH ATTRIBUTES
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Committed
_
MT-Level	MT-Safe
.TE

.SH SEE ALSO
.BR ioctl (2),
.BR malloc (3C),
.BR socket.h (3HEAD),
.BR sockaddr (3SOCKET),
.BR socket (3SOCKET),
.BR if_tcp (4P),
.BR attributes (7),
.BR ifconfig (8),
.BR ipadm (8)

.SH NOTES
This function lists interfaces of type AF_INET, AF_INET6, and AF_LINK.
For AF_INET and AF_INET6 only interfaces with the \fBIFF_UP\fR
flag set are listed; see \fBif_tcp\fR(4P) and \fBifconfig\fR(8) for more
information. For AF_LINK entries the interface index is only available when the
link is plumbed.
